Passed
Pull Request — master (#436)
by
unknown
02:07
created

MattermostNotifier.createReaction   A

Complexity

Conditions 2

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 25
rs 9.4
c 0
b 0
f 0
cc 2
1
import { HttpService } from '@nestjs/axios';
2
import { BadGatewayException, Injectable } from '@nestjs/common';
3
import { ConfigService } from '@nestjs/config';
4
import { IMattermostNotifier } from 'src/Application/IMattermostNotifier';
5
6
@Injectable()
7
export class MattermostNotifier implements IMattermostNotifier {
8
  constructor(
9
    private readonly configService: ConfigService,
10
    private readonly httpService: HttpService
11
  ) {}
12
13
  public async createPost(channelId: string, message: string): Promise<object> {
14
    try {
15
      const response = await this.httpService.axiosRef.post(
16
        `${this.configService.get<string>('MATTERMOST_API_URL')}/posts`,
17
        {
18
          channel_id: channelId,
19
          message
20
        },
21
        {
22
          headers: {
23
            Authorization: `Bearer ${this.configService.get<string>(
24
              'MATTERMOST_ALFRED_TOKEN'
25
            )}`
26
          }
27
        }
28
      );
29
30
      return response.data;
31
    } catch (e) {
32
      throw BadGatewayException;
33
    }
34
  }
35
36
  public async createComment(
37
    channelId: string,
38
    message: string,
39
    rootId: string
40
  ): Promise<object> {
41
    try {
42
      const response = await this.httpService.axiosRef.post(
43
        `${this.configService.get<string>('MATTERMOST_API_URL')}/posts`,
44
        {
45
          channel_id: channelId,
46
          message,
47
          root_id: rootId
48
        },
49
        {
50
          headers: {
51
            Authorization: `Bearer ${this.configService.get<string>(
52
              'MATTERMOST_ALFRED_TOKEN'
53
            )}`
54
          }
55
        }
56
      );
57
58
      console.log(response.data);
59
60
      return response.data;
61
    } catch (e) {
62
      console.log('--error: ', e);
63
      throw BadGatewayException;
64
    }
65
  }
66
67
  public async createReaction(
68
    postId: string,
69
    emojiName: string
70
  ): Promise<object> {
71
    try {
72
      const response = await this.httpService.axiosRef.post(
73
        `${this.configService.get<string>('MATTERMOST_API_URL')}/reactions`,
74
        {
75
          post_id: postId,
76
          emoji_name: emojiName,
77
          user_id: this.configService.get<string>('MATTERMOST_ALFRED_USER_ID')
78
        },
79
        {
80
          headers: {
81
            Authorization: `Bearer ${this.configService.get<string>(
82
              'MATTERMOST_ALFRED_TOKEN'
83
            )}`
84
          }
85
        }
86
      );
87
88
      return response.data;
89
    } catch (e) {
90
      throw BadGatewayException;
91
    }
92
  }
93
}
94